/*
 * 作者：姚俊
 * 功能：论著报表
 * 日期：2015-05-24
 */
package admin.databaseToExcel;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.List;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import org.apache.struts2.ServletActionContext;

import admin.action.fruits.Constant;
import admin.service.fruits.TreatiseService;

import teacher.bean.Treatise;
import teacher.service.treatise;
import tools.StringDate;

public class PullTreatiseToExcel {
	
	
	/**
	 * @category 生成论文的报表
	 * @author yaojun
	 * @param flag int 值为0查询全部、1按照起止日期查询、2按照获奖名称模糊查询
	 * @param treatise1 Treatise 传入一个论文的对象，当查询全部的时候该对象为空，当按照起止日期查询的时候，该对象必须有日期
	 * 当进行名称模糊查询的时候该对象中必须有名称
	 * @param treatise2 当该对象根据日期查询的时候，该对象中放入一个截止日期
	 */
	public static void treatiseExcel(int flag,Treatise treatise1,Treatise treatise2,int direction)
	{
        //论著列表
        List<Treatise> treatises= null;
        try {
            WritableWorkbook wwb = null; 
            String uploadPath = ServletActionContext.getServletContext()
					.getRealPath("/upload");
            
            File dir=new File(uploadPath);  

	        //如果这个目录不存在,则创建它  
	        if (!dir.exists()) {  
	            dir.mkdir();  
	        } 
               // 创建可写入的Excel工作簿
              String fileName = uploadPath+"/"+"treatise.xls";
//            String fileName="D://treatises.xls";
               File file=new File(fileName);
               if (!file.exists()) {
                   file.createNewFile();
               }
               //以fileName为文件名来创建一个Workbook
               wwb = Workbook.createWorkbook(file);
               // 创建工作表
               WritableSheet ws = wwb.createSheet("Test Shee 1", 0);             

               
               if(flag==0)
               {
            	  treatises=new treatise().selectAllTreatise();
               }
               else if(flag==1)
               {
            	   treatises=new treatise().selectTimeTreatise(treatise1.getDate(), treatise2.getDate());
               }
               else if(flag==2)
               {
            	   treatises=new treatise().selectVagueTreatise(treatise1.getName());
               }
               else
               {
            	   if(treatise2!=null&&treatise2.getDate()!=null)
            	   treatises=new TreatiseService().getTreatiseByAll(treatise1, StringDate.DateToString(treatise2.getDate()),direction, Constant.state);
            	   else
            	   treatises=new TreatiseService().getTreatiseByAll(treatise1, null,direction, Constant.state);
               }
               
               
               //要插入到的Excel表格的行号，默认从0开始
               Label tid= new Label(0, 0, "编号");//表示第
               Label tname= new Label(1, 0, "论著名称");//表示第
               Label tauthor= new Label(2, 0, "第一作者");
               Label tclass_= new Label(3, 0, "论著类型");   
               Label tpresse= new Label(4, 0, "论著出版社");
               Label tbookNum= new Label(5, 0, "书号");
               Label tdate= new Label(6, 0, "出版日期");
               
               ws.addCell(tid);
               ws.addCell(tname);
               ws.addCell(tauthor);
               ws.addCell(tclass_);
               ws.addCell(tpresse);
               ws.addCell(tbookNum);
               ws.addCell(tdate);
               

               for (int i = 0; i < treatises.size(); i++) {  
            	   String ee=null;
            	   Label ttid= new Label(0, i+1, i+1+"");
                   Label ttname= new Label(1, i+1, treatises.get(i).getName()+"");
                   Label ttauthor= new Label(2, i+1, treatises.get(i).getAuthor());
                   if(treatises.get(i).getClass_()==0){
                	   ee="教材";
                   }else if(treatises.get(i).getClass_()==1){
                	   ee="专著";
                   }else if(treatises.get(i).getClass_()==2){
                	   ee="编著";
                   }else{
                	   ee="译著";
                   }
                   Label ttclass_= new Label(3, i+1, ee+""); 
                   Label ttpresse= new Label(4, i+1, treatises.get(i).getPress()+"");  
                   Label ttbookNum= new Label(5, i+1, treatises.get(i).getBookNum()+"");  
                   Label ttdate= new Label(6, i+1,
                		   StringDate.DateToString(
                				   new SimpleDateFormat("yyyy-MM-dd").parse(
                						   StringDate.DateToString(
                								   treatises.get(i).getDate()))));    
                   
                   ws.addCell(ttid);
                   ws.addCell(ttname);
                   ws.addCell(ttauthor);
                   ws.addCell(ttclass_);          
                   ws.addCell(ttpresse);
                   ws.addCell(ttbookNum);
                   ws.addCell(ttdate);   
               }  
              //写进文档
               wwb.write();
              // 关闭Excel工作簿对象
               wwb.close();     
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
    }

}
